package com.blueberry.quiz_master.quiz.service;

import com.blueberry.quiz_master.quiz.model.QuestionBankRequest;
import com.blueberry.quiz_master.quiz.model.QuestionBankResponse;
import com.blueberry.quiz_master.redis.RedisService;
import com.blueberry.quiz_master.task.TaskService;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionBankService {
    // 业务类型标识
    private static final String BUSINESS_TYPE = "QUESTION_BANK:";
    @Autowired
    private SensitiveWordBs sensitiveWordBs;
    @Autowired
    private TaskService taskService;  // 依赖任务管理服务
    @Autowired
    private QuestionAsyncService questionAsyncService;
    @Autowired
    private RedisService redisService;

    /**
     * 提交题库生成任务（入口方法）
     */
    public String submitGenerateTask(QuestionBankRequest request) {

        String requestName = request.getName();
        List<String> foundWords = sensitiveWordBs.findAll(requestName);

        if(!foundWords.isEmpty()){
            String firstFoundWord = foundWords.get(0);
            throw new IllegalArgumentException("输入信息包含敏感词["+firstFoundWord+"]等，请修改后重试");
        }
        // 1. 创建任务（通过TaskService，不直接操作Task实体）
        String taskId = taskService.createTask(BUSINESS_TYPE);
        questionAsyncService.generateQuestionBankAsync(taskId, request);
        return taskId;  // 立即返回任务ID
    }

    public QuestionBankResponse getQuestionBank(String bankId) {
        return redisService.getObject(bankId, QuestionBankResponse.class);
    }
}
